//构造函数
function Canvasmap() {
	//最终坐标点
	this.markers=[]
	//缓冲数组
	this.BufferArray=[]
}
//初始化聚点合并
Canvasmap.prototype.init_mapdata = function(data,id,scale){
	var mapdata={}
	this.markers=[]
	data=JSON.parse(data)
	var index=data.findIndex(item=>item.markers.id==id)
	var equipMentdata=data[index]
	data.splice(index,1)
	equipMentdata.markers.quantity=1
	this.init_calculation(data,equipMentdata.markers,scale)
	mapdata.markers=this.markers
	mapdata.equipMentdata=equipMentdata
	return mapdata
};
//初始化计算合并点
Canvasmap.prototype.init_calculation = function(data,markers,scale){
	this.markers=null
	var distance=this.regionchange_scale(scale)
	var res 
	this.markers=markers
	this.BufferArray=[]
	for(let i=0;i<data.length;i++){
		res=this.Calculate_distance(this.markers.longitude,this.markers.latitude,data[i].markers.longitude,data[i].markers.latitude)
		if(res<distance){ 
			console.log(data[i]) 
			this.markers.quantity++
		}
	} 
};
//计算坐标距离
Canvasmap.prototype.Calculate_distance = function(longitude1,latitude1,longitude2,latitude2){
	var radLat1 = latitude1*Math.PI / 180.0;
	var radLat2 = latitude2*Math.PI / 180.0;
	var a = radLat1 - radLat2;
	var  b = longitude1*Math.PI / 180.0 - longitude2*Math.PI / 180.0;
	var s = 2 * Math.asin(Math.sqrt(Math.pow(Math.sin(a/2),2) +
	Math.cos(radLat1)*Math.cos(radLat2)*Math.pow(Math.sin(b/2),2)));
	s = s *6378.137 ;
	s = Math.round(s * 10000) / 10000;
	s = s * 1000 
	return s
};
//缩放时标记点的聚合展开
Canvasmap.prototype.regionchange_distance = function(data,scale){
	var mapdata={}
	this.markers=[]
	for(let i=0;i<data.length;i++){
		data[i].markers.quantity=1//给每个元素新增存储合并数量的变量,默认自己为一个坐标计量
	  this.regionchange_calculation(data[i].markers,scale)
	}
	mapdata.markers=this.markers
	return mapdata
};
//缩放计算合并点
Canvasmap.prototype.regionchange_calculation = function(data,scale){
	var distance=this.regionchange_scale(scale)
	var res 
	this.BufferArray=[]
	//第一次计算坐标数组为空，将第一个元素直接push进去
	if(this.markers.length==0){ 
		this.markers.push(data) 
	}else{
		for(let i=0;i<this.markers.length;i++){
			res=this.Calculate_distance(this.markers[i].longitude,this.markers[i].latitude,data.longitude,data.latitude)
			this.BufferArray.push(res)
		}  
		let index= this.BufferArray.findIndex(item=>item<distance)
		if(index==-1){  
			this.markers.push(data)
		}else{
			this.markers[index].quantity++
		}
	} 
};
//不同缩放级别对于标记之间的距离判断是否合并，如果初始缩放级别不为五请修改判断最后默认返回的值
Canvasmap.prototype.regionchange_scale = function(scale){
	if((3<scale&&scale<4)||scale==4||scale==3){//缩放值3
			return 300000
		}else if((4<scale&&scale<5)||scale==5){//缩放值4....下同
			return 200000
		}else if((5<scale&&scale<6)||scale==6){//注意默认缩放级别为5，如果不是一定修改默认返回值且和缩放级别所对应的值
			 return 90000
		}else if((6<scale&&scale<7)||scale==7){
			return 50000
		}else if((7<scale&&scale<8)||scale==8){
			return 24500
		}else if((8<scale&&scale<9)||scale==9){
			return 12500
		}else if((9<scale&&scale<10)||scale==10){
			return 7000
		}else if((10<scale&&scale<11)||scale==11){
			return 3200
		}else if((11<scale&&scale<12)||scale==12){
			return 1600 
		}else if((12<scale&&scale<13)||scale==13){
			return 850
		}else if((13<scale&&scale<14)||scale==14){
			return 300
		}else if((14<scale&&scale<15)||scale==15){
			return 150
		}else if((15<scale&&scale<16)||scale==16){
			return 70
		}else if((16<scale&&scale<17)||scale==17){
			return 40
		}else if((17<scale&&scale<18)||scale==18){
			return 22
		}else if((18<scale&&scale<19)||scale==19){
			return 9
		}else if((19<scale&&scale<20)||scale==20){
			return 4
		}else {
			return 90000//默认值
		}
};
export default Canvasmap
